import request from "@/utils/request";
import { AxiosPromise } from "axios";
import { RoleQuery, RolePageVO, RoleForm } from "./types";

/**
 * 获取角色数据
 *
 * @param queryParams
 */
export function getRoleList(
  queryParams?: RoleQuery
): AxiosPromise<TreeResult<RolePageVO>[]> {
  return request({
    url: "admin/role/tree",
    method: "get",
    params: queryParams,
  });
}

/**
 * 获取角色的菜单ID集合
 *
 * @param queryParams
 */
export function getRoleMenuIds(roleId: string): AxiosPromise<string[]> {
  const params = { id: roleId };
  return request({
    url: "admin/role/menu",
    method: "get",
    params,
  });
}

/**
 * 分配菜单权限给角色
 *
 * @param data
 */
export function updateRoleMenus(
  roleId: string,
  menuIds: string[]
): AxiosPromise<any> {
  const data = {
    id: roleId,
    menus: menuIds,
  };

  return request({
    url: "admin/role/menu",
    method: "post",
    data,
  });
}

/**
 * 获取角色详情
 *
 * @param id
 */
export function getRoleForm(id: string): AxiosPromise<RoleForm> {
  return request({
    url: "admin/role/" + id,
    method: "get",
  });
}

/**
 * 添加角色
 *
 * @param data
 */
export function saveRole(data: RoleForm) {
  return request({
    url: "admin/role/save",
    method: "post",
    data,
  });
}

/**
 * 批量删除角色，多个以英文逗号(,)分割
 *
 * @param ids
 */
export function deleteRoles(ids: string[]) {
  return request({
    url: "admin/role/del",
    method: "post",
    data: ids,
  });
}

export function getRoleUserInfo(params) {
  const param = {
    roleId: params,
  };
  return request({
    url: "admin/role/getRoleUserInfo",
    method: "get",
    params: param,
  });
}
